﻿using AppMachine.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using AppMachine.Entities;

namespace AppMachine.Controls {
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:AppMachine.Controls"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:AppMachine.Controls;assembly=AppMachine.Controls"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:ButtonAxisCrawl/>
    ///
    /// </summary>
    public class ButtonAxisCrawl : Button {

        static ButtonAxisCrawl() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonAxisCrawl), new FrameworkPropertyMetadata(typeof(ButtonAxisCrawl)));
        }

        protected override void OnRender(DrawingContext drawingContext) {
            this.RefreshUI();
            base.OnRender(drawingContext);
        }

        /// <summary>
        /// 轴线 应用类型
        /// </summary>
        public AxisType AxisType {
            get { return (AxisType)GetValue(AxisTypeProperty); }
            set { SetValue(AxisTypeProperty, value); }
        }
        public static readonly DependencyProperty AxisTypeProperty =
            DependencyProperty.Register("AxisType", typeof(AxisType), typeof(ButtonAxisCrawl), new PropertyMetadata(AxisType.X, (d, e) => {
                (d as ButtonAxisCrawl)?.RefreshUI();
            }));

        /// <summary>
        /// 是否正向(默认True)
        /// X右正左负；Y前正后负；Z上正下负；C/R顺时针正/逆时针负
        /// </summary>
        public bool IsPositive {
            get { return (bool)GetValue(IsPositiveProperty); }
            set { SetValue(IsPositiveProperty, value); }
        }
        public static readonly DependencyProperty IsPositiveProperty =
            DependencyProperty.Register("IsPositive", typeof(bool), typeof(ButtonAxisCrawl), new PropertyMetadata(true, (d, e) => {
                (d as ButtonAxisCrawl)?.RefreshUI();
            }));

        private void RefreshUI() {
            string tempButtonContent = string.Empty;
            if (this.AxisType == AxisType.X) {
                tempButtonContent = this.IsPositive ? "X右移" : "X左移";
            } else if(this.AxisType == AxisType.Y){
                tempButtonContent = this.IsPositive ? "Y前进" : "Y后退";
            } else if (this.AxisType == AxisType.Z) {
                tempButtonContent = this.IsPositive ? "Z上升" : "Z下降";
            } else if (this.AxisType == AxisType.C) {
                tempButtonContent = this.IsPositive ? "C顺转" : "C逆转";
            } else if (this.AxisType == AxisType.R) {
                tempButtonContent = this.IsPositive ? "R顺转" : "R逆转";
            }
            this.Content = tempButtonContent;
        }

    }
}
